﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Sofire.Data.XSql
{
    internal static class XSqlBindType
    {
        public const string None = "none";

        public const string Boolean = "bool";

        public const string Decimal = "decimal";
        public const string Double = "double";
        public const string Single = "float";

        public const string SByte = "sbyte";
        public const string Byte = "byte";

        public const string Int16 = "short";
        public const string Int32 = "int";
        public const string Int64 = "long";

        public const string UInt16 = "ushort";
        public const string UInt32 = "uint";
        public const string UInt64 = "ulong";

        public const string String = "string";
        public const string DateTime = "datetime";

        public static object ChangedType(object value, string type, out System.Data.DbType dbType)
        {
            dbType = System.Data.DbType.String;
            switch(type)
            {
                case XSqlBindType.Boolean:
                    dbType = System.Data.DbType.Boolean;
                    if(value is Boolean) break;
                    else if(value == null || Convert.IsDBNull(value)) return null;
                    value = Convert.ToBoolean(value);
                    break;

                case XSqlBindType.Decimal:
                    dbType = System.Data.DbType.Decimal;
                    if(value is Decimal) break;
                    else if(value == null || Convert.IsDBNull(value)) return null;
                    value = Convert.ToDecimal(value);
                    break;
                case XSqlBindType.Double:
                    dbType = System.Data.DbType.Double;
                    if(value is Double) break;
                    else if(value == null || Convert.IsDBNull(value)) return null;
                    value = Convert.ToDouble(value);
                    break;
                case XSqlBindType.Single:
                    dbType = System.Data.DbType.Single;
                    if(value is Single) break;
                    else if(value == null || Convert.IsDBNull(value)) return null;
                    value = Convert.ToSingle(value);
                    break;

                case XSqlBindType.SByte:
                    dbType = System.Data.DbType.SByte;
                    if(value is SByte) break;
                    else if(value == null || Convert.IsDBNull(value)) return null;
                    value = Convert.ToSByte(value);
                    break;
                case XSqlBindType.Byte:
                    dbType = System.Data.DbType.Byte;
                    if(value is Byte) break;
                    else if(value == null || Convert.IsDBNull(value)) return null;
                    value = Convert.ToByte(value);
                    break;

                case XSqlBindType.Int16:
                    dbType = System.Data.DbType.Int16;
                    if(value is Int16) break;
                    else if(value == null || Convert.IsDBNull(value)) return null;
                    value = Convert.ToInt16(value);
                    break;
                case XSqlBindType.Int32:
                    dbType = System.Data.DbType.Int32;
                    if(value is Int32) break;
                    else if(value == null || Convert.IsDBNull(value)) return null;
                    value = Convert.ToInt32(value);
                    break;
                case XSqlBindType.Int64:
                    dbType = System.Data.DbType.Int64;
                    if(value is Int64) break;
                    else if(value == null || Convert.IsDBNull(value)) return null;
                    value = Convert.ToInt64(value);
                    break;

                case XSqlBindType.UInt16:
                    dbType = System.Data.DbType.UInt16;
                    if(value is UInt16) break;
                    else if(value == null || Convert.IsDBNull(value)) return null;
                    value = Convert.ToUInt16(value);
                    break;
                case XSqlBindType.UInt32:
                    dbType = System.Data.DbType.UInt32;
                    if(value is UInt32) break;
                    else if(value == null || Convert.IsDBNull(value)) return null;
                    value = Convert.ToUInt32(value);
                    break;
                case XSqlBindType.UInt64:
                    dbType = System.Data.DbType.UInt64;
                    if(value is UInt64) break;
                    else if(value == null || Convert.IsDBNull(value)) return null;
                    value = Convert.ToUInt64(value);
                    break;
                case XSqlBindType.DateTime:
                    dbType = System.Data.DbType.DateTime;
                    if(value is DateTime) break;
                    else if(value == null || Convert.IsDBNull(value)) return null;
                    value = Convert.ToDateTime(value);
                    break;
                case XSqlBindType.None:
                case XSqlBindType.String:
                    if(value is String) break;
                    else if(value == null || Convert.IsDBNull(value)) return null;
                    value = Convert.ToString(value);
                    break;
            }
            return value;
        }
    }
}
